package com.qing.common.ktx

import android.content.Context

/**
 * 获取资源ID
 * @param resName 资源名称
 * @param resType 资源类型
 * @return 资源ID
 */
inline fun <reified T : Context> T.getResourceId(resName: String, resType: String): Int =
    applicationContext.resources.getIdentifier(
        resName,
        resType,
        applicationContext.packageName
    )

/**
 * 运行时捕获异常
 * @param block 运行代码块
 * @param error 错误回调
 */
inline fun runCatchSilence(block: () -> Unit, noinline error: ((Exception) -> Unit)? = null) {
    try {
        block()
    } catch (e: Exception) {
        error?.invoke(e)
    }
}

inline fun blockTime(block: () -> Unit): Long {
    val start = System.currentTimeMillis()
    block()
    return System.currentTimeMillis() - start
}